﻿using System;
using System.Collections.Generic;
using System.Text;
using JKZP.Model;
using System.Data;
using System.Data.OracleClient;

namespace JKZP.DAL
{
    public class ZPCLXXDAL
    {
        private ZPCLXX ToCustomer(DataRow row)
        {
            ZPCLXX zpclxx = new ZPCLXX();
            zpclxx.Hphm = (string)row["HPHM"];
            //zpclxx.Zpsj = (string)row["ZPSJ"];
            return zpclxx;
        }
        public ZPCLXX[] GetAll()
        {
            DataTable table = new DataTable();
            table = SqlHelper.ExecuteDataTable("select hphm,zpsj,zpr from zpclxx");
            ZPCLXX[] zpclxxs = new ZPCLXX[table.Rows.Count];
            for (int i = 0; i < table.Rows.Count; i++)
            {
                DataRow row = table.Rows[i];
                zpclxxs[i] = ToCustomer(row);
            }
            return zpclxxs;
        }

        public string GetById(string hphm, string wfdd, string zpsj)
        {
            DataTable dt = SqlHelper.ExecuteDataTable(@"select zpsj from zpclxx 
                    where hphm=:hphm and wfdd=:wfdd and zpsj like to_date(:zpsj,'yyyy-mm-dd')",
                new OracleParameter(":hphm", hphm),
                new OracleParameter(":wfdd",wfdd),
                new OracleParameter(":zpsj", zpsj));
            if (dt.Rows.Count <= 0)
            {
                return null;
            }
            else if (dt.Rows.Count > 1)
            {
                throw new Exception("严重错误，查出多条数据！");
            }
            else
            {
                DataRow row = dt.Rows[0];
                string value = row[0].ToString();
                return value;
            }
        }

        public int Insert(ZPCLXX zpclxx)
        {
            int returnValue = 0;
            //to_date(:JGSJ,'yyyy-mm-dd hh24:mi:ss')
            returnValue = SqlHelper.ExecuteNonQuery(@"INSERT INTO zpclxx
                        (HPHM, WFDD, ZPSJ, ZPR,SCBJ)
                 VALUES
                       (:HPHM, :WFDD, :ZPSJ, :ZPR, :SCBJ)",

                new OracleParameter(":HPHM", zpclxx.Hphm),
                new OracleParameter(":WFDD", zpclxx.Wfdd),
                new OracleParameter(":ZPSJ", zpclxx.Zpsj),

                new OracleParameter(":ZPR", zpclxx.Zpr),
                new OracleParameter(":SCBJ", zpclxx.Scbj));
            return returnValue;

        }
        public int Update(ZPCLXX zpclxx)
        {
            int returnValue = 0;
            returnValue = SqlHelper.ExecuteNonQuery(@"UPDATE zpclxx
                        SET SCBJ = :SCBJ
                           where HPHM=:HPHM and ZPR=:ZPR and ZPSJ like :ZPSJ",
                new OracleParameter(":SCBJ", zpclxx.Scbj),
                new OracleParameter(":HPHM", zpclxx.Hphm),
                new OracleParameter(":ZPR", zpclxx.Zpr),
                new OracleParameter(":ZPSJ", zpclxx.Zpsj));

            return returnValue;
        }

    }
}
